package com.eduhzy.ttw.work.mvp.ui.adapter;

import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonsdk.http.CommonApi;
import com.eduhzy.ttw.work.R;
import com.eduhzy.ttw.work.mvp.model.entity.WorkSubmitData;
import com.luck.picture.lib.config.PictureMimeType;

import org.simple.eventbus.EventBus;

import java.util.List;

import me.jessyan.progressmanager.ProgressListener;
import me.jessyan.progressmanager.ProgressManager;
import me.jessyan.progressmanager.body.ProgressInfo;

/**
 * @author xubo
 * @date 2018/12/1 10:55
 */
public class WorkUploadAdapter extends BaseQuickAdapter<WorkSubmitData, AutoBaseViewHolder> {
    private Handler mHandler;

    public WorkUploadAdapter(@Nullable List<WorkSubmitData> data) {
        super(R.layout.work_rv_item_upload, data);
        mHandler = new Handler();
    }

    @Override
    protected void convert(AutoBaseViewHolder helper, WorkSubmitData item) {
        helper.addOnClickListener(R.id.btn_delete);
        item.setPosition(helper.getLayoutPosition());
        int mimeType = item.getLocalMedia().getMimeType();
        ProgressBar mUploadProgress = helper.getView(R.id.progress);
        if (item.isUploadComplete()) {
            mUploadProgress.setProgress(100);
            helper.setText(R.id.tv_progress, "100%");
        }
        if (mimeType == PictureMimeType.ofAudio()) {
            helper.setGone(R.id.btn_play, false);
            helper.setImageResource(R.id.iv_icon, R.drawable.audio_placeholder);
        } else {
            if (mimeType == PictureMimeType.ofVideo()) {
                helper.setGone(R.id.btn_play, true);
            } else if (mimeType == PictureMimeType.ofImage()) {
                helper.setGone(R.id.btn_play, false);
            }
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.public_color_F6F6F6)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(helper.itemView.getContext())
                    .load(item.getLocalMedia().getPath())
                    .apply(options)
                    .into((ImageView) helper.getView(R.id.iv_icon));
        }
        if (!item.isUploadComplete() && !item.getType().equals(WorkSubmitData.ERROR)) {
            //Okhttp/Retofit 上传监听
            String url = ProgressManager.getInstance().addDiffRequestListenerOnSameUrl(CommonApi.APP_UPLOAD_DOMAIN + CommonApi.APP_UPLOAD_PATH, getUploadListener(helper, item));
            item.setUrl(url);
            Message msg = Message.obtain();
            msg.obj = item;
            msg.what = EventBusTags.START_UPLOAD;
            EventBus.getDefault().post(msg, RouterHub.WORK_PUBLISHWORKACTIVITY);
        }
    }

    @NonNull
    private ProgressListener getUploadListener(AutoBaseViewHolder helper, WorkSubmitData item) {
        ProgressBar mUploadProgress = helper.getView(R.id.progress);
        TextView mUploadProgressText = helper.getView(R.id.tv_progress);
        return new ProgressListener() {
            @Override
            public void onProgress(ProgressInfo progressInfo) {
                item.setUploading(true);
                int progress = progressInfo.getPercent();
                mUploadProgress.setProgress(progress);
                mUploadProgressText.setText(progress + "%");
                if (progressInfo.isFinish()) {
                    item.setUploading(false);
                    item.setUploadComplete(true);
                    Message msg = Message.obtain();
                    msg.what = EventBusTags.END_UPLOAD;
                    EventBus.getDefault().post(msg, RouterHub.WORK_PUBLISHWORKACTIVITY);

                    //说明已经加载完成
                    Log.d(TAG, "--Glide-- finish");
                }
            }

            @Override
            public void onError(long id, Exception e) {
                item.setUploading(false);
                mHandler.post(() -> {
                    mUploadProgress.setProgress(0);
                    item.setType(WorkSubmitData.ERROR);
                    mUploadProgressText.setText("error");
                    Message msg = Message.obtain();
                    msg.what = EventBusTags.END_UPLOAD;
                    EventBus.getDefault().post(msg, RouterHub.WORK_PUBLISHWORKACTIVITY);
                });
            }
        };
    }
}

